package com.doodod.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.Proxy;

/**
 * 此类是根据扩展名返回ContentType类型
 * Created with IntelliJ IDEA.
 * User: ydw
 * Date: 15-4-3
 * Time: 上午10:36
 */
public class HttpUtil {

    public static String setContentType(String returnFileName) {

        String contentType = "application/octet-stream";
        if (returnFileName.lastIndexOf(".") < 0)
            return contentType;
        returnFileName = returnFileName.toLowerCase();
        returnFileName = returnFileName.substring(returnFileName.lastIndexOf(".") + 1);

        if (returnFileName.equals("html") || returnFileName.equals("htm") || returnFileName.equals("shtml")) {
            contentType = "text/html";
        } else if (returnFileName.equals("apk")) {
            contentType = "application/vnd.android.package-archive";
        } else if (returnFileName.equals("sis")) {
            contentType = "application/vnd.symbian.install";
        } else if (returnFileName.equals("sisx")) {
            contentType = "application/vnd.symbian.install";
        } else if (returnFileName.equals("exe")) {
            contentType = "application/x-msdownload";
        } else if (returnFileName.equals("msi")) {
            contentType = "application/x-msdownload";
        } else if (returnFileName.equals("css")) {
            contentType = "text/css";
        } else if (returnFileName.equals("xml")) {
            contentType = "text/xml";
        } else if (returnFileName.equals("gif")) {
            contentType = "image/gif";
        } else if (returnFileName.equals("jpeg") || returnFileName.equals("jpg")) {
            contentType = "image/jpeg";
        } else if (returnFileName.equals("js")) {
            contentType = "application/x-javascript";
        } else if (returnFileName.equals("atom")) {
            contentType = "application/atom+xml";
        } else if (returnFileName.equals("rss")) {
            contentType = "application/rss+xml";
        } else if (returnFileName.equals("mml")) {
            contentType = "text/mathml";
        } else if (returnFileName.equals("txt")) {
            contentType = "text/plain";
        } else if (returnFileName.equals("jad")) {
            contentType = "text/vnd.sun.j2me.app-descriptor";
        } else if (returnFileName.equals("wml")) {
            contentType = "text/vnd.wap.wml";
        } else if (returnFileName.equals("htc")) {
            contentType = "text/x-component";
        } else if (returnFileName.equals("png")) {
            contentType = "image/png";
        } else if (returnFileName.equals("tif") || returnFileName.equals("tiff")) {
            contentType = "image/tiff";
        } else if (returnFileName.equals("wbmp")) {
            contentType = "image/vnd.wap.wbmp";
        } else if (returnFileName.equals("ico")) {
            contentType = "image/x-icon";
        } else if (returnFileName.equals("jng")) {
            contentType = "image/x-jng";
        } else if (returnFileName.equals("bmp")) {
            contentType = "image/x-ms-bmp";
        } else if (returnFileName.equals("svg")) {
            contentType = "image/svg+xml";
        } else if (returnFileName.equals("jar") || returnFileName.equals("var") || returnFileName.equals("ear")) {
            contentType = "application/java-archive";
        } else if (returnFileName.equals("doc")) {
            contentType = "application/msword";
        } else if (returnFileName.equals("pdf")) {
            contentType = "application/pdf";
        } else if (returnFileName.equals("rtf")) {
            contentType = "application/rtf";
        } else if (returnFileName.equals("xls")) {
            contentType = "application/vnd.ms-excel";
        } else if (returnFileName.equals("ppt")) {
            contentType = "application/vnd.ms-powerpoint";
        } else if (returnFileName.equals("7z")) {
            contentType = "application/x-7z-compressed";
        } else if (returnFileName.equals("rar")) {
            contentType = "application/x-rar-compressed";
        } else if (returnFileName.equals("swf")) {
            contentType = "application/x-shockwave-flash";
        } else if (returnFileName.equals("rpm")) {
            contentType = "application/x-redhat-package-manager";
        } else if (returnFileName.equals("der") || returnFileName.equals("pem") || returnFileName.equals("crt")) {
            contentType = "application/x-x509-ca-cert";
        } else if (returnFileName.equals("xhtml")) {
            contentType = "application/xhtml+xml";
        } else if (returnFileName.equals("zip")) {
            contentType = "application/zip";
        } else if (returnFileName.equals("mid") || returnFileName.equals("midi") || returnFileName.equals("kar")) {
            contentType = "audio/midi";
        } else if (returnFileName.equals("mp3")) {
            contentType = "audio/mpeg";
        } else if (returnFileName.equals("ogg")) {
            contentType = "audio/ogg";
        } else if (returnFileName.equals("m4a")) {
            contentType = "audio/x-m4a";
        } else if (returnFileName.equals("ra")) {
            contentType = "audio/x-realaudio";
        } else if (returnFileName.equals("3gpp") || returnFileName.equals("3gp")) {
            contentType = "video/3gpp";
        } else if (returnFileName.equals("mp4")) {
            contentType = "video/mp4";
        } else if (returnFileName.equals("mpeg") || returnFileName.equals("mpg")) {
            contentType = "video/mpeg";
        } else if (returnFileName.equals("mov")) {
            contentType = "video/quicktime";
        } else if (returnFileName.equals("flv")) {
            contentType = "video/x-flv";
        } else if (returnFileName.equals("m4v")) {
            contentType = "video/x-m4v";
        } else if (returnFileName.equals("mng")) {
            contentType = "video/x-mng";
        } else if (returnFileName.equals("asx") || returnFileName.equals("asf")) {
            contentType = "video/x-ms-asf";
        } else if (returnFileName.equals("wmv")) {
            contentType = "video/x-ms-wmv";
        } else if (returnFileName.equals("avi")) {
            contentType = "video/x-msvideo";
        }

        return contentType;
    }


    /**
     *  HttpGet请求
     */
    public static String downloadHtml(String url) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        StringBuilder urlStringBuilder = new StringBuilder(url);
        String entity = null;
        //利用URL生成一个HttpGet请求
        HttpGet httpGet = new HttpGet(urlStringBuilder.toString());
        HttpResponse httpResponse = null;
        try {
            //HttpClient发出一个HttpGet请求
            httpResponse = httpclient.execute(httpGet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //得到httpResponse的状态响应码
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            //得到httpResponse的实体数据
            HttpEntity httpEntity = httpResponse.getEntity();
            try {
                entity = EntityUtils.toString(httpEntity);
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            return entity;
        }
        return null;
    }

    /**
     * 从给定的url下载文件。
     *
     * @param destDir
     * @param url
     * @return
     */
    public static File downloadFileFromUrl(String destDir, String url) {
        try {
            String[] urlLevel = url.split("/");
            String fileName = urlLevel[urlLevel.length - 1];

            File file = new File(destDir, fileName);
            if (!file.exists()) {
                CloseableHttpClient httpclient = HttpClients.createDefault();

                // 创建GET方法的实例
                HttpGet httpGet = new HttpGet(url);

                CloseableHttpResponse response = null;
                //获取返回结果
                response = httpclient.execute(httpGet);
                HttpEntity entity = response.getEntity();
                file.createNewFile();
                FileOutputStream output = new FileOutputStream(file);
                //得到网络资源的字节数组,并写入文件
                entity.writeTo(output);
                output.close();
                return file;
            } else {
                return file;
            }
        } catch (IOException e) {
            System.out.println(url + "——下载失败");
        }
        return null;
    }


    /**
     * 得到com.alibaba.fastjson.JSON
     *
     * @param urlString
     * @return
     */
    public static JSONObject getJsonFromUrl(String urlString) {
        JSONObject resultJsonObject = null;
        if (urlString == null || "".equals(urlString)) {
            return null;
        }
        CloseableHttpClient httpclient = HttpClients.createDefault();
        StringBuilder urlStringBuilder = new StringBuilder(urlString);
        String entity = null;
        //利用URL生成一个HttpGet请求
        HttpGet httpGet = new HttpGet(urlStringBuilder.toString());
        HttpResponse httpResponse = null;
        try {
            //HttpClient发出一个HttpGet请求
            httpResponse = httpclient.execute(httpGet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //得到httpResponse的状态响应码
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            //得到httpResponse的实体数据
            HttpEntity httpEntity = httpResponse.getEntity();
            try {
                entity = EntityUtils.toString(httpEntity);
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            //利用从HttpEntity中得到的String生成JsonObject
            resultJsonObject = JSONObject.parseObject(entity);
        }

        return resultJsonObject;
    }





    /**
     * 使用代理服务器进行web请求
     * @param urlTmp
     * @return
     */
    public static String userAgent(String urlTmp){
        String respond = "";

        String Http_Agent_Value = "Mozilla/5.0 (Linux; U; Android 4.1.1; " +
                "zh-cn; Nexus S Build/ID) AppleWebKit/534.30 " +
                "(KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 " +
                "MicroMessenger/6.1.0.66_r1062275.542 NetType/WIFI";
        HttpURLConnection conn = null;

        try {
            URL url = new URL(urlTmp);
            conn = (HttpURLConnection) url.openConnection();
            if (null==conn){
                return null;
            }
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("User-agent", Http_Agent_Value);
            conn.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String lines = "";

            while ((lines = reader.readLine()) != null) {
                respond += lines;
            }
            reader.close();
            conn.disconnect();
        } catch (Exception e) {
            //e.printStackTrace();
            respond = e.getMessage();
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
            conn = null;
        }
        return respond;
    }

    public static String getHtml(String address){
        StringBuffer html = new StringBuffer();
        String result = null;
        try{
            URL url = new URL(address);
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident /4.0; SV1; QQDownload 732; .NET4.0C;.NET4 .0E; 360SE)");
            conn.setAllowUserInteraction(true);
            conn.setConnectTimeout(3000);
            conn.setDefaultUseCaches(true);
            conn.setDoOutput(true);
            conn.setUseCaches(true);

            BufferedInputStream in = new BufferedInputStream(conn.getInputStream());

            try {
                String inputLine;
                byte[] buf = new byte[4096];
                int bytesRead = 0;
                while (bytesRead >= 0) {
                    inputLine = new String(buf, 0, bytesRead, "ISO-8859-1");
                    html.append(inputLine);
                    bytesRead = in.read(buf);
                    inputLine = null;
                }
                buf = null;
            }catch (Exception e) {
                e.printStackTrace();
                return null;
            }finally{
                in.close();
                conn = null;
                url = null;
            }
            result = new String(html.toString().trim().getBytes("ISO-8859-1"), "gb2312").toLowerCase();

        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally{
            html = null;
        }
        return result;
    }

    public static String getHtmlUtf8(String urlTmp){
        String respond = "";
        String Http_Agent_Value = "Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn; Nexus S Build/ID) "+
                "AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/6.1.0.66_r1062275.542 NetType/WIFI";
        HttpURLConnection conn = null;

        try {
            URL url = new URL(urlTmp);
            conn = (HttpURLConnection) url.openConnection();
            if (null==conn){
                return null;
            }
            conn.setConnectTimeout(2000);
            conn.setReadTimeout(2000);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("User-agent", Http_Agent_Value);
            conn.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String lines = "";

            while ((lines = reader.readLine()) != null) {
                respond += lines;
            }
            reader.close();
            conn.disconnect();
        } catch (Exception e) {
            //e.printStackTrace();
            respond = e.getMessage();
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
            conn = null;
        }
        return respond;
    }


    public static String getHtmlUtf8(String urlTmp, String httpUserAgent){
        String respond = "";
        HttpURLConnection conn = null;

        try {
            URL url = new URL(urlTmp);
            conn = (HttpURLConnection) url.openConnection();
            if (null==conn){
                return null;
            }
            conn.setConnectTimeout(2000);
            conn.setReadTimeout(2000);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("User-agent", httpUserAgent);
            conn.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String lines = "";

            while ((lines = reader.readLine()) != null) {
                respond += lines;
            }
            reader.close();
            conn.disconnect();
        } catch (Exception e) {
            respond = e.getMessage();
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
            conn = null;
        }
        return respond;
    }

    public static String getPageUtf8(String urlTmp, Proxy proxy, String httpUserAgent){
        String respond = "";
        HttpURLConnection conn = null;

        try {
            URL url = new URL(urlTmp);
            conn = (HttpURLConnection) url.openConnection(proxy);
            if (null==conn){
                return null;
            }
            conn.setConnectTimeout(2000);
            conn.setReadTimeout(2000);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("User-agent", httpUserAgent);
            conn.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String lines = "";

            while ((lines = reader.readLine()) != null) {
                respond += lines;
            }
            reader.close();
            conn.disconnect();
        } catch (Exception e) {
            respond = e.getMessage();
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
            conn = null;
        }
        return respond;
    }


    public static void main(String[] args){

        // 通过文章url爬取文章信息，入库
        String url = "http://weixin.sogou.com/weixinjs?query=Bose%E5%8D%9A%E5%A3%AB&fr=sgsearch&tsn=1&sut=1095&sourceid=inttime_day&interation=&type=2&interV=kKIOkrELjbkRmLkElbkTkKIMkrELjboImLkEk74TkKIRmLkEk78TkKILkbELjboN_105333196&ie=utf8&sst0=1436336949030&cid=null";
        String agent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0)";
        String html = HttpUtil.getHtmlUtf8(url, agent);
        System.out.println(html);
        System.out.println("sadas");
    }


}
